package com.ethink.warehouse.service.impl;

import com.ethink.warehouse.api.info.MateInventoryOrderMoreInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.warehouse.api.info.MateInventoryOrderInfo;
import com.ethink.warehouse.api.request.MateInventoryOrderAddRequest;
import com.ethink.warehouse.api.request.MateInventoryOrderQueryRequest;
import com.ethink.warehouse.api.request.MateInventoryOrderUpdateRequest;
import com.ethink.warehouse.api.response.MateInventoryOrderAddResponse;
import com.ethink.warehouse.api.response.MateInventoryOrderDeleteResponse;
import com.ethink.warehouse.api.response.MateInventoryOrderUpdateResponse;
import com.ethink.warehouse.common.entity.MateInventoryOrder;
import com.ethink.warehouse.common.mapper.MateInventoryOrderMapper;
import com.ethink.warehouse.converter.MateInventoryOrderConverter;
import com.ethink.warehouse.service.MateInventoryOrderService;
import java.util.List;
import org.springframework.beans.BeanUtils;

/**
 * 入库/出库单 Service接口实现
 *
 * @author default
 * @date 2022-12-15
 */
@Slf4j
@Service
public class MateInventoryOrderServiceImpl implements MateInventoryOrderService {

    @Autowired
    private MateInventoryOrderMapper mateInventoryOrderMapper;

    @Autowired
    private MateInventoryOrderConverter mateInventoryOrderConverter;

    @Override
    public Page<MateInventoryOrderMoreInfo> queryMateInventoryOrder(MateInventoryOrderQueryRequest request) {
        return mateInventoryOrderMapper.queryMateInventoryOrder(PageUtils.buildPage(request), request);
    }

    @Override
    public MateInventoryOrderInfo getMateInventoryOrderById(Long mateInventoryOrderId) {
        return mateInventoryOrderMapper.getMateInventoryOrderById(mateInventoryOrderId);
    }

    @Override
    public List<MateInventoryOrderMoreInfo> listMateInventoryOrder(MateInventoryOrderQueryRequest request) {
        return mateInventoryOrderMapper.listMateInventoryOrder(request);
    }

    @Override
    public MateInventoryOrderAddResponse addMateInventoryOrder(MateInventoryOrderAddRequest request) {
        MateInventoryOrderAddResponse response = new MateInventoryOrderAddResponse();
        MateInventoryOrder mateInventoryOrder = new MateInventoryOrder();
        BeanUtils.copyProperties(request, mateInventoryOrder);
        // FIXME set默认字段
        mateInventoryOrderMapper.insert(mateInventoryOrder);
        return response;
    }

    @Override
    public MateInventoryOrderUpdateResponse updateMateInventoryOrder(MateInventoryOrderUpdateRequest request) {
        MateInventoryOrderUpdateResponse response = new MateInventoryOrderUpdateResponse();
        LambdaUpdateWrapper<MateInventoryOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateInventoryOrder::getId, request.getId());
        // FIXME 按实际业务需求set
        updateWrapper.set(MateInventoryOrder::getOrderNo, request.getOrderNo());
        updateWrapper.set(MateInventoryOrder::getOrderType, request.getOrderType());
        updateWrapper.set(MateInventoryOrder::getSourceOrderType, request.getSourceOrderType());
        updateWrapper.set(MateInventoryOrder::getSubject, request.getSubject());
        updateWrapper.set(MateInventoryOrder::getSubWarehouseCode, request.getSubWarehouseCode());
        updateWrapper.set(MateInventoryOrder::getCreateUserCode, request.getCreateUserCode());
        updateWrapper.set(MateInventoryOrder::getCreateUserName, request.getCreateUserName());
        mateInventoryOrderMapper.update(null, updateWrapper);
        return response;
    }

    @Override
    public MateInventoryOrderDeleteResponse deleteMateInventoryOrder(Long mateInventoryOrderId) {
        MateInventoryOrderDeleteResponse response = new MateInventoryOrderDeleteResponse();
        LambdaUpdateWrapper<MateInventoryOrder> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateInventoryOrder::getId, mateInventoryOrderId);
        updateWrapper.set(MateInventoryOrder::getIsDeleted, true);
        mateInventoryOrderMapper.update(null, updateWrapper);
        return response;
    }

}
